package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    private final WxPayService wxPayService;
    @Autowired
    public WxPayApiController(WxPayService wxPayService) {
        this.wxPayService = wxPayService;
    }
    /**
     * @description: 当选择微信下单时 会调用此接口
     * @author Petrichor
     * @date 2024/1/9 23:18
     * @param paymentType "支付类型：1301-订单 1302-充值"
     * @param orderNo 订单号
     * @return WeChatPayVo 请求微信JSAPI下单接口响应数据模型
    **/
    @GuiGuLogin
    @Operation(summary = "微信下单")
    @Parameters({
            @Parameter(name = "paymentType",description = "支付类型：1301-订单 1302-充值",in = ParameterIn.PATH,required = true),
            @Parameter(name = "orderNo",description = "订单号",required = true,in = ParameterIn.PATH),
    })
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result<WeChatPayVo> createJsapi(@PathVariable String paymentType, @PathVariable String orderNo) {
        WeChatPayVo weChatPayVo = wxPayService.createJsapi(paymentType, orderNo);
        return Result.ok(weChatPayVo);
    }
    /**
     * @description: 根据订单号 查询支付状态查询
     * @author Petrichor
     * @date 2024/1/10 19:50
     * @param orderNo 订单号
     * @return 支付状态查询
    **/
    @Operation(summary = "支付状态查询")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        boolean result = wxPayService.queryPayStatus(orderNo);
        return Result.ok(result);
    }
    /**
     * @description: 微信支付异步通知接口
     * @author Petrichor
     * @date 2024/1/10 20:56
     * @return
    **/
    @Operation(summary = "微信支付异步通知接口")
    @PostMapping("/notify")
    public ResponseEntity<Map<String , String>> notify(HttpServletRequest request){
        Map<String , String> result = new HashMap<>() ;
        try {
            wxPayService.wxnotify(request);
            result.put("code" , "SUCCESS") ;
            result.put("message" , "成功") ;
            return ResponseEntity.status(HttpStatus.OK).body(result);
        }catch (Exception e) {
            e.printStackTrace();
            result.put("code" , "FAIL") ;
            result.put("message" , "失败") ;
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result) ;
        }
    }
}
